package com.iflytek.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iflytek.common.Result;
import com.iflytek.dto.BannerDTO;
import com.iflytek.dto.PageDTO;
import com.iflytek.mapper.BannerMapper;
import com.iflytek.model.Banner;
import com.iflytek.service.BannerService;
import com.iflytek.service.RoleService;
import com.iflytek.utils.CommonUtil;
import com.iflytek.utils.CopyUtil;
import com.iflytek.vo.BannerVO;
import com.iflytek.vo.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 权限菜单表 服务实现类
 * </p>
 *
 * @author h_t
 * @since 2023-09-23
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements BannerService {

    @Resource
    private BannerMapper bannerMapper;
    @Resource
    private BannerService bannerService;

    @Resource
    private RoleService roleService;

    /**
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<List<BannerVO>> findAll() {
        QueryWrapper<Banner> queryWrapper = new QueryWrapper<>();
        List<Banner> bannerList = bannerMapper.selectList(queryWrapper);
        List<BannerVO> bannerVOList = CopyUtil.copyList(bannerList, BannerVO.class);
        return Result.success(bannerVOList);
    }

    /**
     * saveBanner
     * @param dto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Boolean> saveBanner(BannerDTO dto) {
        if (dto == null) {
            return Result.error("1", "内容为空");
        }
        Banner banner = CopyUtil.copy(dto, Banner.class);
        if (StringUtils.isEmpty(banner.getId())) {
            if (StringUtils.isEmpty(banner)) {
                return Result.error("1", "保存失败");
            }
        }
        bannerMapper.insert(banner);
        return Result.success(true);
    }

    /**
     * updateBanner
     * @param dto
     * @return
     */
    @Override
    public Result<Boolean> updateBanner(BannerDTO dto) {
        Banner banner = new Banner();
        BeanUtils.copyProperties(dto, banner);
        int count = bannerMapper.updateById(banner);
        if (count > 0) {
            return Result.success(true);
        } else {
            return Result.error("1", "更新失败");
        }
    }

    /**
     * @param id
     * @return
     */
    @Override
    public Result<BannerVO> findById(Integer id) {

        if (id == null) {
            Result.error("401", "id为空");
        }
        Banner banner = bannerMapper.selectById(id);
        BannerVO bannerVO = CopyUtil.copy(banner, BannerVO.class);

        return Result.success(bannerVO);
    }


    /**
     * @param id
     * @return
     */
    @Override
    public Result<BannerVO> deleteById(Integer id) {

        if (id == null) {
            Result.error("401", "id为空");
        }
        int i = bannerMapper.deleteById(id);
        if (i > 0) {
            Result.success();
        } else {

            Result.error("204", "错误");
        }
        return Result.success();
    }

    /**
     * @param dto
     * @return
     */
    @Override
    public Result<PageVO<BannerVO>> findPage(PageDTO<BannerDTO> dto) {
        QueryWrapper<Banner> queryWrapper = new QueryWrapper();
        if (!CommonUtil.isEmpty(dto.getName())) {
            queryWrapper.lambda().like(Banner::getUrl, dto.getName());
        }
        if (dto.getPageNum() == null) {
            dto.setPageNum(1);
        }
        if (dto.getSize() == null) {
            dto.setSize(5);
        }
        //分页
        PageHelper.startPage(dto.getPageNum(), dto.getSize());

        List<Banner> banners = bannerMapper.selectList(queryWrapper);

        List<BannerVO> bannerVOList = new ArrayList<>();
        bannerVOList = CopyUtil.copyList(banners, BannerVO.class);

        PageInfo<Banner> bannerPageInfo = new PageInfo<>(banners);
        PageVO<BannerVO> pageVO = new PageVO<>();
        BeanUtils.copyProperties(dto, pageVO);
        pageVO.setTotal(bannerPageInfo.getTotal());
        pageVO.setRecords(bannerVOList);
        return Result.success(pageVO);
    }

}

